package com.yugao.fintech.antelope.system.manager;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.system.mapper.SysArticleGroupMapper;
import com.yugao.fintech.antelope.system.model.entity.SysArticleGroup;
import com.yugao.fintech.framework.assistant.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 文章组 仓储层
 *

 */
@Slf4j
@Repository
public class SysArticleGroupManager extends ServiceImpl<SysArticleGroupMapper, SysArticleGroup>
    implements IService<SysArticleGroup> {

    /**
     * 添加文章数量
     * @param groupId 组id
     */
    public void addArticleNum(Long groupId) {
        if (Objects.isNull(groupId)) {
            return;
        }
        LambdaUpdateWrapper<SysArticleGroup> luw = SysArticleGroup.luw().eq(SysArticleGroup::getId, groupId)
                .setSql("article_num = article_num + 1");
        this.update(luw);

    }

    /**
     * 减少文章数量
     * @param groupIds 组id
     */
    public void batchDecreaseArticleNum(List<Long> groupIds) {
        if (CollectionUtils.isEmpty(groupIds)) {
            return;
        }
        LambdaUpdateWrapper<SysArticleGroup> luw = SysArticleGroup.luw().in(SysArticleGroup::getId, groupIds)
                .gt(SysArticleGroup::getArticleNum, 0)
                .setSql("article_num = article_num - 1");
        this.update(luw);
    }

    public void decreaseArticleNum(Long groupId) {
        if (Objects.isNull(groupId)) {
            return;
        }
        batchDecreaseArticleNum(Collections.singletonList(groupId));
    }
}
